<template>
  <!-- 添加或修改客户回款单对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="订单编号" prop="orderCode">
                <el-select v-model="form.orderCode" filterable remote reserve-keyword placeholder="请输入订单编号" style="width: 100%;"
                           :remote-method="searchOrder" :loading="orderLoading" clearable>
                  <el-option v-for="item in orderList" :key="item.code" @click.native="changeOrder(item)" :label="item.code" :value="item.code">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="客户名称" prop="customerName" disabled="disabled">
                <el-input v-model="form.customerName" placeholder="请输入客户名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="收款金额" prop="money">
                <el-input-number v-model="form.money" placeholder="请输入收款金额"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, getAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';

export default {
  name: "FinanceInvoiceReceiptForm",
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [{required: true, message: "请选择订单", trigger: "blur"}],
        money: [
          {required: true, message: "回款金额不能为空", trigger: "blur"},
          {validator: this.moneyValidator, trigger: 'blur'}],
      },
      url: {
        getById: '/financeInvoiceReceipt/financeInvoiceReceipt/',
        add: '/financeInvoiceReceipt/financeInvoiceReceipt/add',
        edit: '/financeInvoiceReceipt/financeInvoiceReceipt/edit',
      },
      orderList:[],
      orderLoading: false,
    };
  },
  created() {

  },
  methods: {
    moneyValidator(rule, value, callback) {
      if (value === 0) {
        callback(new Error('收款金额不能为0'));
      } else {
        callback();
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        customerName: null,
        orderId: null,
        orderCode: null,
        money: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户回款单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户回款单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },


    async searchOrder(queryString) {
      this.orderLoading = true;
      if (queryString) {
        try {
          let params = { "pageNum": 1, "pageSize": 20, "code": queryString };
          getAction("/projectBaseInfo/projectBaseInfo/list", params).then(response => {
            this.orderList = response.rows;
            this.orderLoading = false;
          });

        } catch (error) {
          console.error('Error fetching data:', error);
          this.orderLoading = false;
        }
      } else {
        this.orderList = [];
        this.orderLoading = false;
      }
    },


    changeOrder(item) {
      let that = this;
      that.form.orderId = item.id;
      that.$set(that.form, "customerId", item.customId);
      that.$set(that.form, "customerName", item.customName);
    },






  }
};
</script>
